<?php
    use User\Model\DailySignModel;
?>
<style>
    .item{width:200px;height:200px;margin-left:20px;margin-top:20px;list-style: none;float:left;}
    .item-add{text-align: center;height:120px;line-height: 120px;border:1px dashed black}
    .item-add a{width:100%;height:100%;color:black;text-decoration: none;display: block}
    .item-title{width:100%;text-align: center;height:80px;line-height: 80px;border:1px solid black;color:black;
        background-color:#F2F2F2;cursor:pointer}
    .item-edit,.item-delete{width:98px;text-align: center;height:40px;line-height: 40px;border:1px solid black;
        display: inline-block;}
    .item-edit a,.item-delete a{color:black;text-decoration: none;display: block}
    .item-creator,.item-time{text-align: left;font-weight: bold;margin-top:10px}
</style>
<div class="page-heading">
    <h3><if condition="$type eq DailySignModel::ATTACH_TYPE_WORD">语录库
        <elseif condition="$type eq DailySignModel::ATTACH_TYPE_IMG"/>图库<else/>宜忌文库</if>
    </h3>
</div>
<div class="row">
    <div class="col-lg-12">
        <div class="panel">
            <!--<div class="panel-heading">-->
                <!--<a style="margin-left:7px" href="/User/DailySign/" class="btn btn-custom color3">新增</a>-->
                <!--<form class="input-group" action="/User/DailySign/attach?type={$type}" style="width:100%;float:right">-->
                    <!--<input type="text" class="form-control" style="width:200px" name="title" placeholder="输入语录库名称搜索" value="{$title}">-->
                    <!--<input type="text" class="form-control" style="width:200px" name="creator" placeholder="输入作者搜索" value="{$author}">-->
                    <!--<span class="input-group-btn">-->
							<!--<button class="btn btn-default" type="submit">-->
								<!--<i class="fa fa-search"></i>-->
							<!--</button>-->
						<!--</span>-->
                <!--</form>-->
            <!--</div>-->
            <div class="panel-body">
                <div class="col-lg-12">
                    <ul>
                        <li class="item item-add">
                            <div><a href="javascript:;" onclick="addAttach()">+ 新建分组</a></div>
                        </li>
                        <volist name="list" id="lt">
                            <li class="item">
                                <div class="item-title" data-id="{$lt.id}"><i class="fa fa-edit"></i> {$lt.title}</div>
                                <div class="item-edit"><a href="/User/DailySign/attachment?attach_id={$lt.id}">编辑</a></div>
                                <div class="item-delete"><a href="javascript:;" onclick="statusAttach({$lt['id']})">删除</a></div>
                                <div class="item-creator">作者：{$lt.creator}</div>
                                <div class="item-time">建立时间：{$lt.createdate}</div>
                            </li>
                        </volist>
                    </ul>
                    <div class="pages">
                        {$page}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="attach-title" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">修改语录库名称</h4>
            </div>
            <div class="modal-body form-horizontal">
                <div class="form-group">
                    <label class="col-sm-2 col-sm-2 control-label">名称</label>
                    <div class="col-lg-6">
                        <input type="hidden" name="attach_id">
                        <input type="text" class="form-control" title="名称" name="attach_title">
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" onclick="updateAttachTitle()">确定</button>
            </div>
        </div>
    </div>
</div>
<div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="attach" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">语录库</h4>
            </div>
            <div class="modal-body form-horizontal">
                <div class="form-group">
                    <label class="col-sm-2 col-sm-2 control-label">名称</label>
                    <div class="col-lg-6">
                        <input type="text" class="form-control" title="名称" name="title" value="">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 col-sm-2 control-label">是否共享</label>
                    <div class="col-lg-6">
                        <label class="radio-inline">
                            <input type="radio" name="has_share" checked="checked" value="<?php echo DailySignModel::HAS_SHARE_NO; ?>"> 否
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="has_share" value="<?php echo DailySignModel::HAS_SHARE_YES; ?>">&nbsp;是
                        </label>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" onclick="updateAttach()">确定</button>
            </div>
        </div>
    </div>
</div>

<script>
    $('.item-title').click(function(){
        $('#attach-title').modal('show');
        $('input[name=attach_id]').val($(this).data('id'));
        $('input[name=attach_title]').val($(this).text());
    });

    function updateAttachTitle(){
        var attach_id = $('input[name=attach_id]').val();
        var attach_title = $('input[name=attach_title]').val();
        if(attach_title === ''){
            alert('名称不能为空');
            return false;
        }
        $.ajax({
            url: "/User/DailySign/updateAttachTitle",
            type: "post",
            dataType: 'json',
            data: {attach_title: attach_title,attach_id: attach_id},
            success:function(result){
                if(result['code'] === 0){
                    alert('提交成功');
                    window.location.reload();
                }
            }
        });
    }

    function addAttach(){
        $('#attach').modal('show');
    }

    function updateAttach(){
        var title = $('input[name=title]').val();
        var has_share = $('input[name=has_share]:checked').val();
        var type = '{$type}';
        if(title === ''){
            alert('名称不能为空');
            return false;
        }
        $.ajax({
            url: "/User/DailySign/addAttach",
            type: "post",
            dataType: 'json',
            data: {title: title,has_share: has_share,type: type},
            success:function(result){
                if(result['code'] === 0){
                    alert('提交成功');
                    window.location.reload();
                }
            }
        });
    }

    function statusAttach(id){
        if(!confirm('确认删除吗？')) return false;
        $.ajax({
            data: {id: id},
            dataType: 'json',
            url: '/User/DailySign/statusAttach',
            type: 'get',
            success: function (res) {
                alert(res['info']);
                window.location.reload();
            }
        });
        return false;
    }
</script>